package com.platform.modules.report.controller;


import com.platform.common.utils.MapUtil;
import com.platform.common.utils.NumberUtils;
import com.platform.modules.report.dao.PrjReportDao;
import com.platform.modules.report.dto.*;
import com.platform.modules.report.service.CptAccReportService;
import com.platform.modules.report.service.PrjReportService;
import com.platform.modules.report.service.QlyAccReportService;
import com.platform.modules.report.service.QlyInsReportService;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.dao.SysCityAreaDao;
import com.platform.modules.sys.entity.SysCityAreaEntity;
import com.platform.modules.sys.service.SysDictService;
import com.platform.web.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api(tags = "报表 - 综合统计分析")
@RestController
@RequestMapping("report/main")
public class MainReportController extends BaseReportController {
    @Autowired
    PrjReportDao prjReportDao;
    @Autowired
    PrjReportService prjReportService;
    @Autowired
    QlyInsReportService qlyInsReportService;
    @Autowired
    QlyAccReportService qlyAccReportService;
    @Autowired
    CptAccReportService cptAccReportService;
    @Autowired
    SysDictService sysDictService;


    @ApiOperation("红色横批 - [项目总数:prjCount | 竣工工程数:cptCount | 中止施工数:pauseCount]")
    @PostMapping("banner")
    public CommonResult<Map<String,Object>> banner(@RequestBody BaseQueryDTO baseQueryDTO){

        Integer prjCount = prjReportService.prjCount(baseQueryDTO);
        Integer cptCount = cptAccReportService.cptAccCount(baseQueryDTO);
        Integer pauseCount = prjReportService.pauseCount(baseQueryDTO);

        return CommonResult.success(MapUtil.of("prjCount",prjCount,"cptCount",cptCount,"pauseCount",pauseCount));
    }


    @ApiOperation("表格 - 各市综合统计：[项目总数|总面积|总投资]")
    @PostMapping("prjInfoByCity")
    public CommonResult<List<PrjInfoDTO>> prjSumByCity(@RequestBody BaseQueryDTO baseQueryDTO){

        Map<String,Object> prjCountMap = prjReportService.prjCountGroupByCity(baseQueryDTO);
        Map<String,Object> prjTotalAreaMap =  prjReportService.prjTotalAreaGroupByCity(baseQueryDTO);
        Map<String,Object> prjTotalInvestmentMap = prjReportService.prjTotalInvestmentGroupByCity(baseQueryDTO);

        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());

        List<PrjInfoDTO> data = new ArrayList<>(areas.size());
        areas.forEach(e->{
            String xzqh = e.getId();
            Integer prjCount = NumberUtils.intValueOf0(prjCountMap.get(xzqh));
            Object totalArea_ = prjTotalAreaMap.get(xzqh);
            Double totalArea = 0D;
            if(totalArea_!=null){
                totalArea = ((BigDecimal)totalArea_).doubleValue();
            }

            Object totalInvestment_ = prjTotalInvestmentMap.get(xzqh);
            Double totalInvestment = 0D;
            if(totalInvestment_!=null){
                totalInvestment = ((BigDecimal)totalInvestment_).doubleValue();
            }
            data.add(new PrjInfoDTO(xzqh, e.getName(), prjCount, totalArea, totalInvestment));
        });
        return CommonResult.success(data);
    }


    @ApiOperation("各市质量巡查数 - 默认:柱图")
    @PostMapping("qlyInsCountGroupByCity")
    public CommonResult<List<BaseItemDTO>> qlyInsCountGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        Map<String,Object> map = qlyInsReportService.qlyInsCountGroupByCity(baseQueryDTO);
        List<BaseItemDTO> data = this.buildAreaItem(map, areas);
        return CommonResult.success(data);
    }


    @ApiOperation("各市质量验收数 - 默认:柱图")
    @PostMapping("qlyAccCountGroupByCity")
    public CommonResult<List<BaseItemDTO>> qlyAccCountGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        Map<String,Object> map = qlyAccReportService.qlyAccCountGroupByCity(baseQueryDTO);
        List<BaseItemDTO> data = this.buildAreaItem(map, areas);
        return CommonResult.success(data);
    }

    @ApiOperation("各市竣工验收数 - 默认:柱图")
    @PostMapping("cptAccCountGroupByCity")
    public CommonResult<List<BaseItemDTO>> cptAccCountGroupByCity(@RequestBody BaseQueryDTO baseQueryDTO){
        List<SysCityAreaEntity> areas = this.getAreas(baseQueryDTO.getProvinceNum());
        Map<String,Object> map = cptAccReportService.cptAccCountGroupByCity(baseQueryDTO);
        List<BaseItemDTO> data = this.buildAreaItem(map, areas);
        return CommonResult.success(data);
    }


    @ApiOperation("企业top5 - 默认:柱图")
    @PostMapping("sgdwPrjCountTop5")
    public CommonResult<List<BaseItemDTO>> sgdwPrjCountTop5(@RequestBody BaseQueryDTO baseQueryDTO){
        return CommonResult.success(prjReportDao.sgdwPrjCountTop5(baseQueryDTO));
    }

}
